<template>
  <div>
    <div class="tools-div">
      <el-button type="success" size="small" @click="dialogVisible = true">
        添 加
      </el-button>
    </div>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="name" label="品牌名称" />
      <el-table-column prop="logo" label="品牌图标" #default="scope">
        <img :src="scope.row.logo" width="50" />
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="200" #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="handleShowEidtDialog(scope.row)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[1, 2, 3, 4, 5]"
      layout="total, sizes, prev, pager, next"
      @size-change="fetchData"
      @current-change="fetchData"
      :total="total"
    />
    <!-- 新增修改品牌对话框 -->
    <div>
      <el-dialog v-model="dialogVisible" title="添加或修改" width="30%">
        <el-form label-width="120px">
          <el-form-item label="品牌名称">
            <el-input v-model="brand.name" placeholder="请输入品牌名称" />
          </el-form-item>
          <el-form-item label="品牌图标">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleUploadSuccess"
              :headers="headers"
              :data="{ module: 'brand' }"
            >
              <img
                v-if="brand.logo"
                :src="brand.logo"
                class="avatar"
                style="width:178px;height:178px"
              />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveOrUpdateBrand">
              提交
            </el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import {
  FindBrandPage,
  SaveBrand,
  UpdateBrand,
  GetBrandById,
  DeleteBrandById,
} from '@/api/brand'
import { ElMessage } from 'element-plus'
import { useApp } from '@/pinia/modules/app'
let list = ref([])
let total = ref(0)
let pageNum = ref(1)
let pageSize = ref(3)

let dialogVisible = ref(false)
let brand = ref({})

let headers = ref({
  token: useApp().authorization.token,
})
let uploadUrl = ref('http://localhost:8501/admin/system/file/upload') //上传接口访问时也需要在请求头中携带token

let handleUploadSuccess = response => {
  //上传组件提交请求时没有经过request.js中的响应拦截器处理
  // 当前方法回调时 不一定上传成功
  if (+response.code === 200) {
    ElMessage.success('上传成功')
    console.log(response.data)
    console.log(response.code)
    brand.value.logo = response.data
  } else {
    ElMessage.error(response.message)
  }
}

let fetchData = async () => {
  const { data } = await FindBrandPage(pageNum.value, pageSize.value)
  total.value = data.total
  list.value = data.list
}

onMounted(() => {
  fetchData()
})
//删除按钮事件函数
let handleDelete = async row => {
  await DeleteBrandById(row.id)
  ElMessage.success('删除成功')
  //刷新页面
  fetchData()
}
//修改按钮事件函数
let handleShowEidtDialog = async row => {
  const { data } = await GetBrandById(row.id)
  brand.value = data
  dialogVisible.value = true
}
let handleSaveOrUpdateBrand = () => {
  if (brand.value.id) {
    //更新
    handleUpdateBrand()
  } else {
    //新增
    handleSaveBrand()
  }
  brand.value = {}
}
let handleSaveBrand = async () => {
  await SaveBrand(brand.value)
  ElMessage.success('新增成功')
  dialogVisible.value = false
  fetchData()
}
let handleUpdateBrand = async () => {
  await UpdateBrand(brand.value)
  ElMessage.success('更新成功')
  dialogVisible.value = false
  fetchData()
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
